package cn.itcast.web.controller.system;

import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.SysDict;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.SysDictService;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/dict")
public class SysDictController extends BaseController {

   @Autowired
   private SysDictService sysDictService;

   /**
    * 分页查询
    */
   @RequestMapping("/list")
   public String list(
         @RequestParam(defaultValue = "1") int pageNum,
         @RequestParam(defaultValue = "5") int pageSize) {
      PageInfo pageInfo = sysDictService.findByPage(pageNum, pageSize);
      request.setAttribute("pageInfo",pageInfo);
      return "system/dict/dict-list";
   }

   /**
    * 进入修改页面
    */
   @RequestMapping("/toUpdate")
   public String toUpdate(String id, Model model) {
      // 根据id查询
      SysDict dict = sysDictService.findById(id);
      // 保存
      model.addAttribute("dict", dict);
      return "system/dict/dict-update";
   }

   /**
    * 添加或修改
    */
   @RequestMapping("/edit")
   public String edit(SysDict sysDict) {
      // 设置企业信息（从登陆用户中获取）


      // 判断：根据id是否为空判断执行添加还是修改
      if (StringUtils.isEmpty(sysDict.getId())) {
         // 添加
         sysDictService.save(sysDict);
      } else {
         // 修改
         sysDictService.update(sysDict);
      }

      return "redirect:/system/dict/list";
   }

   /**
    * 删除
    */
   @RequestMapping("/delete")
   public String delete(String id) {
      sysDictService.delete(id);
      return "redirect:/system/dict/list";
   }


   /**
    * 进入添加页面
    */
   @RequestMapping("/toAdd")
   public String toAdd() {
      return "system/dict/dict-add";
   }
}